﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Threading;
namespace TD
{
    public enum ButtonType
    {
        Left = 0,
        Right = 1,
    }


    public delegate void ButtonClick(ButtonType button, Vector2 point);
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class MousePointer : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private SpriteBatch spriteBatch;
        public Vector2 MousePosition;

        private MouseState CurrentMouseState;
        private MouseState PreviousMouseState;

        private Color pointerColor = Color.White;

        private Texture2D pointerTexture;

        public Texture2D PointerTexture
        {
            get { return pointerTexture; }
            set { pointerTexture = value; }
        }

        private Rectangle restrictZone;

        public Rectangle RestrictZone
        {
            get { return restrictZone; }
            set { restrictZone = value; }
        }

        public event ButtonClick OnClick;

        public MousePointer(Game game)
            : base(game)
        {
            beginClick = new Dictionary<ButtonType, bool>();
            beginClick[ButtonType.Left] = false;
            beginClick[ButtonType.Right] = false;
            // TODO: Construct any child components here
        }


        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
           
            base.Initialize();
        }
        protected override void LoadContent()
        {
            
            base.LoadContent();
        }

        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                // TODO: Load any ResourceManagementMode.Automatic content
                this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
            }

            // TODO: Load any ResourceManagementMode.Manual content
            base.LoadGraphicsContent(loadAllContent);
        }

        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            base.UnloadGraphicsContent(unloadAllContent);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            this.CurrentMouseState = Mouse.GetState();
            this.MousePosition.X = this.CurrentMouseState.X;
            this.MousePosition.Y = this.CurrentMouseState.Y;

            // Restrict the Mouse so that it stays inside the current display
            if (this.MousePosition.X < 0)
                this.MousePosition.X = 0;
            if (this.MousePosition.X > this.restrictZone.Width)
                this.MousePosition.X = this.restrictZone.Width;
            if (this.MousePosition.Y < 0)
                this.MousePosition.Y = 0;
            if (this.MousePosition.Y > this.restrictZone.Height)
                this.MousePosition.Y = this.restrictZone.Height;

            if (this.CurrentMouseState.LeftButton == ButtonState.Pressed)
            {
                this.pointerColor = Color.Red;
            }
            else
            {
                this.pointerColor = Color.White;
            }

            if (this.CurrentMouseState.RightButton == ButtonState.Pressed)
            {
                this.pointerColor = Color.Red;
            }
            else
            {
                this.pointerColor = Color.White;
            }

            this.PreviousMouseState = this.CurrentMouseState;

            if (OnClick != null)
            {
                if (!beginButtonClick)
                {
                    foreach (var item in beginClick.Keys.ToArray())
                    {
                        if (item == ButtonType.Left && CurrentMouseState.LeftButton == ButtonState.Pressed)
                        {
                            beginClick[item] = true;
                            ButtonClickPoint.X = CurrentMouseState.X;
                            ButtonClickPoint.Y = CurrentMouseState.Y;
                            beginButtonClick = true;
                            break;
                        }
                        else if (item == ButtonType.Right && CurrentMouseState.RightButton == ButtonState.Pressed)
                        {
                            beginClick[item] = true;
                            ButtonClickPoint.X = CurrentMouseState.X;
                            ButtonClickPoint.Y = CurrentMouseState.Y;
                            beginButtonClick = true;
                            break;
                        }
                        else
                        {
                            beginClick[item] = false;
                            //beginButtonClick = false;
                        }
                    }
                   

                }
                else
                {
                    foreach (var item in beginClick.Keys.ToArray())
                    {
                        if (item == ButtonType.Left && CurrentMouseState.LeftButton == ButtonState.Released)
                        {
                            OnClick(ButtonType.Left, ButtonClickPoint);
                        }
                        else if (item == ButtonType.Right && CurrentMouseState.RightButton == ButtonState.Released)
                        {
                            OnClick(ButtonType.Right, ButtonClickPoint);
                        }
                    }
                    beginButtonClick = false;
                }
            }
            base.Update(gameTime);
        }
        Dictionary<ButtonType, bool> beginClick;
        bool beginButtonClick = false;
        Vector2 ButtonClickPoint;

        /// <summary>
        /// Allows the game commponent to Draw itself
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values</param>
        public override void Draw(GameTime gameTime)
        {
            this.spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            this.spriteBatch.Draw(this.pointerTexture, this.MousePosition, this.pointerColor);
            this.spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

